/* $RCSFile: DateManager.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/DateManager/DateManager.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:52  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Configuration;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Helpers;
using AddonNice.Design;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Security;
using ADNCRM.ServerControls;

using AddonNice.CRMWrapper;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.DateManager
{
    /// <summary>
    /// Initializer class for DateManager 
    /// </summary>
    public class DateManagerSI : PortalCRMModuleControlSI
    {
        public DateManagerSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]DateManagerSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]DateManagerSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]DateManagerSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                       =   pm.ExtendedResourcesGroup;
            
			SettingItemGroup _Group				=	SettingItemGroup.MISC_SETTINGS;
			int _groupOrderBase					=	(int)_Group;
			// Dasplay date
			SettingItemSpecial ShowDate			=	new SettingItemSpecial("ADNCRM_SHOW_DATE",new BooleanDataType());
			ShowDate.Required					=	false;
			ShowDate.Value						=	"True";
			ShowDate.ExtendedGroupName          =    extgrp;
            ShowDate.Order						=	_groupOrderBase++;
			ShowDate.EnglishName				=	"Show Date";
			ShowDate.Description				=	"Set to show the date control and enable date changed events.";
            bSettings.AddSetting(ShowDate);

			SettingItemSpecial ShowSortOrder	=	new SettingItemSpecial("ADNCRM_SHOW_SORTORDER",new BooleanDataType());
			ShowSortOrder.Required				=	false;
			ShowSortOrder.Value					=	"True";
			ShowSortOrder.ExtendedGroupName     =    extgrp;
            ShowSortOrder.Order					=	_groupOrderBase++;
			ShowSortOrder.EnglishName			=	"Show SortOrder ListBox";
			ShowSortOrder.Description			=	"Set to show the sort order Listbox and enable ListBox changed events.";
            bSettings.AddSetting(ShowSortOrder);

			SettingItemSpecial ShowFilter		=	new SettingItemSpecial("ADNCRM_SHOW_FILTER",new BooleanDataType());
			ShowFilter.Required					=	false;
			ShowFilter.Value					=	"True";
			ShowFilter.ExtendedGroupName        =    extgrp;
            ShowFilter.Order					=	_groupOrderBase++;
			ShowFilter.EnglishName				=	"Show Filter field";
			ShowFilter.Description				=	"Set to show the Filter field and enable Filter changed events.";
            bSettings.AddSetting(ShowFilter);

			SettingItemSpecial ShowPageSize		=	new SettingItemSpecial("ADNCRM_SHOW_PAGESIZE",new BooleanDataType());
			ShowPageSize.Required				=	false;
			ShowPageSize.Value					=	"True";
			ShowPageSize.ExtendedGroupName      =    extgrp;
            ShowPageSize.Order					=	_groupOrderBase++;
			ShowPageSize.EnglishName			=	"Show Page Size field";
			ShowPageSize.Description			=	"Set to show the Page Size field and enable Page Size changed events.";
            bSettings.AddSetting(ShowPageSize);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]DateManagerSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
	///	Input Date and selection parameters
	/// </summary>
    public partial class DateManager : PortalCRMModuleControl 
	{
		public override Guid GuidID 
		{
			get
			{
				return new Guid("{D9E590FA-1D75-41a2-9480-56D4BF1ED8DD}");
			}
		}

		public DateManager()
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]DateManager Ctor Called.",ModuleTraceSwitch.Sw.Info);
		}

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp                 =   "DATEMANAGER";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new DateManagerSI(bSettings,this);
        }

		private void Page_Load(object sender, System.EventArgs e)
		{
            ADNPage p                               =   ADNPage;
			if ( !p.IsPostBack ) 
			{
				// To recover from page to page
				if ( !p.ExistQueryDate )
						p.QueryDate					=	DateTime.Now.AddDays(1-DateTime.Now.Day);
				else	DatePicker2.SelectedDate	=	p.QueryDate;
				string strList						=	p.SortOrderList;
				if ( strList != string.Empty )
				{
					string[] OrderStr	            =	strList.Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries);
					foreach(string str in OrderStr )
						DDLSortOrder.Items.Add(str);
				}
				else DDLSortOrder.Visible	        =	false;
				TBPageSize.Text				        =	p.PageSize.ToString();
				TBFilter.Text					    =	p.Filter;
				if ( !Settings["ADNCRM_SHOW_DATE"].ToBool() )
                {
                    LblDate.Visible                 =   false;
                    DatePicker2.Visible             =   false;
                }
				if ( !Settings["ADNCRM_SHOW_SORTORDER"].ToBool() )
                {
                    LblSortOrder.Visible            =   false;
                    DDLSortOrder.Visible            =   false;
                    DDLSortDirection.Visible        =   false;
                }
				if ( !Settings["ADNCRM_SHOW_FILTER"].ToBool() )
                {
                    LblFilter.Visible               =   false;
                    TBFilter.Visible                =   false;
                }
				if ( !Settings["ADNCRM_SHOW_PAGESIZE"].ToBool() )
                {
                    LblPageSize.Visible             =   false;
                    TBPageSize.Visible              =   false;
                    BtnRefresh.Visible              =   false;
                }
			}
            if ( DatePicker2.Visible && (p._GlobalPane != null) )
            {
                AddAjaxSetting(DatePicker2,DatePicker2);
                AddAjaxSetting(DatePicker2,DDLSortOrder);
                AddAjaxSetting(DatePicker2,DDLSortDirection);
                AddAjaxSetting(DatePicker2,TBFilter);
                AddAjaxSetting(DatePicker2,TBPageSize);
                Control pn                          =   p.GetPane(StStr.leftPane);
                if ( pn != null )
                    AddAjaxSetting(DatePicker2,pn);
                pn                                  =   p.GetPane(StStr.contentPane);
                if ( pn != null )
                    AddAjaxSetting(DatePicker2,pn);
                pn                                  =   p.GetPane(StStr.rightPane);
                if ( pn != null )
                    AddAjaxSetting(DatePicker2,pn);
                }
		}

		
        void DatePicker2_SelectionChanged(object sender, Telerik.Web.UI.Calendar.SelectedDatesEventArgs e)
		{
			QueryDate	=	e.SelectedDates[0].Date;
		}

		private void BtnRefresh_Click(object sender, System.EventArgs e)
		{
			Filter		    =	TBFilter.Text;
			try
			{
				PageSize	=	int.Parse(TBPageSize.Text);
			}
			catch(Exception )
			{}
		}

		private void DDLSortOrder_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SortOrder	=	DDLSortOrder.SelectedItem.Value;
		}

		private void DDLSortDirection_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SortDirection	=	DDLSortDirection.SelectedItem.Value;
		}

		private void p_QueryDateChangedEvent(QueryDateChangedEventArgs e)
		{
			if ( DatePicker2.SelectedDate != e.NewTime )
				DatePicker2.SelectedDate = e.NewTime;
		}

		private void p_SortOrderChangedEvent(SortOrderChangedEventArgs e)
		{
			if ( DDLSortOrder.SelectedItem.Value != e.NewSortOrder )
			{
				DDLSortOrder.SelectedValue = e.NewSortOrder;
			}
		}

		private void p_FilterChangedEvent(FilterChangedEventArgs e)
		{
			if ( TBFilter.Text != e.NewFilter )
			{
				TBFilter.Text = e.NewFilter;
			}
		}

		private void p_SortDirectionChangedEvent(SortDirectionChangedEventArgs e)
		{
			if ( DDLSortDirection.SelectedItem.Value != e.NewSortDirection )
			{
				DDLSortDirection.SelectedValue = e.NewSortDirection;
			}
		}

		private void p_PageSizeChangedEvent(PageSizeChangedEventArgs e)
		{
			if ( TBPageSize.Text != e.NewPageSize.ToString() )
			{
				TBPageSize.Text = e.NewPageSize.ToString();
			}
		}

        #region StdOverrides
		
        override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		
		private void InitializeComponent()
		{
            DatePicker2.SelectionChanged            +=  new Telerik.Web.UI.Calendar.SelectedDatesEventHandler(DatePicker2_SelectionChanged);
			BtnRefresh.Click                        +=  new System.EventHandler(BtnRefresh_Click);
			DDLSortOrder.SelectedIndexChanged       +=  new System.EventHandler(DDLSortOrder_SelectedIndexChanged);
			DDLSortDirection.SelectedIndexChanged   +=  new System.EventHandler(DDLSortDirection_SelectedIndexChanged);
			Load                                    +=      new System.EventHandler(Page_Load);
			ADNPage p	                            =	ADNPage;
			if ( p != null )
			{
				p.QueryDateChangedEvent	            +=  new QueryDateChangedEventHandler(p_QueryDateChangedEvent);
				p.SortOrderChangedEvent             +=  new SortOrderChangedEventHandler(p_SortOrderChangedEvent);
				p.FilterChangedEvent                +=  new FilterChangedEventHandler(p_FilterChangedEvent);
				p.PageSizeChangedEvent              +=  new PageSizeChangedEventHandler(p_PageSizeChangedEvent);
				p.SortDirectionChangedEvent         +=  new SortDirectionChangedEventHandler(p_SortDirectionChangedEvent);
			}
		}

		#endregion
	}
}
